1475C - Ball in Berland - CodeForces Solution


combinatorics graphs math *1400

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define ull unsigned long long
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define rep(i, m, n) for (auto i = m; i < n; i++)
#define ppi pair<int, int>
#define ppl pair<ll, ll>
#define um(x, y) unordered_map<x, y>
#define us(x) unordered_set<x>
#define pb push_back
#define endl "\n"
#define all(v) (v).begin(), (v).end()
#define f first
#define INF 1000000000
#define ss second
#define lb lower_bound
#define up upper_bound
#define SET(n) cout << fixed << setprecision(n)
#define pi (double)3.14159265358
#define get(s) getline(cin, s)

ll binexp(ll a, ll b, ll m)
{
    ll res = 1;
    while (b > 0)
    {
        if (b & 1)
        {
            res = (res * 1LL * a) % m;
        }
        a = (a * 1LL * a) % m;
        b >>= 1;
    }
    return res;
}

ll binpow(ll a, ll b)
{
    if (b == 0)
        return 1;
    ll res = binpow(a, b / 2);
    if (b % 2)
        return res * res * a;
    else
        return res * res;
}
bool isPrime(long long int n)
{
    if (n <= 1)
        return false;

    for (long long int i = 2; i <= sqrtl(n); i++)
        if (n % i == 0)
            return false;

    return true;
}
ll modFact(ll n, ll p)
{
    if (n >= p)
        return 0;

    ll result = 1;
    for (int i = 1; i <= n; i++)
        result = (result * i) % p;

    return result;
}
ll count_max_power_of_2_greaterthan_n(ll n)
{
    ll res = 0, ans = 0;
    for (ll i = 1; i <= 60; i++)
    {
        ans = (1LL) << i;
        if (ans > n)
        {
            res = i;
            return res;
        }
    }
    return res;
}

void solve()
{
    ll x,y,k;
    cin>>x>>y>>k;
    ll a[k],b[k];
    map<ll,ll>mp,mpp;
    map<pair<ll,ll>,ll>m;
    rep(i,0,k) cin>>a[i],mp[a[i]]++;
    rep(i,0,k) cin>>b[i],mpp[b[i]]++,m[{a[i],b[i]}]++;
    ll ans=0;
    for(int i=0;i<k-1;i++)
    {
        ll p=mp[a[i]];
        ll q=mpp[b[i]];
        ll r=m[{a[i],b[i]}];
        ll z=(k-i)-(p-1)-(q-1)+r-1;
        if(z>0)
        {
            ans+=z-1;
            // cout<<(z*(z-1))/2<<endl;
        }
        mp[a[i]]--;
        mpp[b[i]]--;
        if(m.find({a[i],b[i]})!=m.end())
        {
           m[{a[i],b[i]}]--;
        }
    }
    cout<<ans<<endl;
}

int main()
{
    fast;
    ll T;
    cin >> T;
    // int T = 1;
    while (T > 0)
    {
        solve();
        T--;
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

1659D - Reverse Sort Sum
1659A - Red Versus Blue
1659B - Bit Flipping
1480B - The Great Hero
1519B - The Cake Is a Lie
1659C - Line Empire
515A - Drazil and Date
1084B - Kvass and the Fair Nut
1101A - Minimum Integer
985D - Sand Fortress
1279A - New Year Garland
1279B - Verse For Santa
202A - LLPS
978A - Remove Duplicates
1304A - Two Rabbits
225A - Dice Tower
1660D - Maximum Product Strikes Back
1513A - Array and Peaks
1251B - Binary Palindromes
768B - Code For 1
363B - Fence
991B - Getting an A
246A - Buggy Sorting
884A - Book Reading
1180A - Alex and a Rhombus
445A - DZY Loves Chessboard
1372A - Omkar and Completion
159D - Palindrome pairs
981B - Businessmen Problems
1668A - Direction Change